package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.qf.pojo.entity.TbAdmin;
import com.qf.pojo.entity.TbUser;
import com.qf.pojo.request.TbAccountReq;
import com.qf.pojo.request.TbAdminReq;
import com.qf.pojo.request.TbAdminRoleReq;
import com.qf.pojo.response.BaseResponse;
import com.qf.service.TbAdminService;
import com.qf.service.TbUserService;
import com.qf.utils.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.Column;
import java.util.List;
import java.util.Map;

/**
 * @author xingyu
 */
@RestController
@RequestMapping("/admin")
@Api(description = "管理员控制器")
public class AdminController {

    @Autowired
    TbAdminService tbAdminService;

    @Autowired
    UploadUtils uploadUtils;
    @ApiOperation(value = "查看所有管理员")
    @RequestMapping("/findAll")
    @RequiresPermissions({"ADMIN_FINDALL"})
    public BaseResponse findAll(@RequestParam("page")Integer page,@RequestParam("size")Integer size){
        return tbAdminService.findAll(page,size);
    }

    @ApiOperation(value = "修改或者新怎管理员")
    @PostMapping("/updateOrSave")
    @RequiresPermissions({"ADMIN_UPDATEORSAVE"})
    public BaseResponse updateOrSave(@RequestBody TbAdminReq tbAdminReq) {
        return tbAdminService.updateOrSave(tbAdminReq);
    }

    @ApiOperation(value = "删除管理员")
    @PostMapping("/deleteById")
    @RequiresPermissions({"ADMIN_DELETEBYID"})
    public BaseResponse del(@RequestBody TbAdminReq tbAdminReq) {
       return tbAdminService.deleteById(tbAdminReq.getAdId());
    }
    @ApiOperation(value = "通过用户id查询管理员对应的角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "map",value = "前端传来的管理员adId",required = true,dataType = "Map")
    })
    @RequestMapping("/findRoleByAdminId")
    @RequiresPermissions({"ADMIN_FINDROLE"})
    public BaseResponse findRoleByUserId(@RequestBody Map map){
        return tbAdminService.findRoleByAdminId(Integer.valueOf(map.get("adId").toString()));
    }
    @ApiOperation(value = "修改管理员角色或管理用户角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbAdminRoleReq",value = "管理员角色请求对象",required = true,dataType = "TbAdminRoleReq")
    })
    @RequestMapping("/updateAdminRole")
    @RequiresPermissions({"ADMIN_FINDROLE"})
    public BaseResponse updateAdminRole(@RequestBody TbAdminRoleReq tbAdminRoleReq){
        return tbAdminService.updateAdminRole(tbAdminRoleReq);
    }
    @ApiOperation(value = "管理员登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbAdminRoleReq",value = "管理员角色请求对象",required = true,dataType = "TbAdminRoleReq")
    })
    @RequestMapping("/login")
    public BaseResponse login(@RequestBody TbAdminReq tbAdminReq){
        return tbAdminService.login(tbAdminReq);
    }
    @ApiOperation(value = "判断管理员是否登录")
    @RequestMapping("/isAuthentication")
    public BaseResponse isAuthentication (){
        //获取对象
        Subject subject = SecurityUtils.getSubject();
        //判断是否登录
        if (subject.isAuthenticated()){
            return new BaseResponse(1,"登录成功",subject.getPrincipal(),null);
        }
        return new BaseResponse().FAILED();
    }
    @ApiOperation(value = "退出登录")
    @RequestMapping("/loginOut")
    public BaseResponse loginOut(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new BaseResponse().SUCCESS();
    }




}
